package com.roc.steelend.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.user.RegisteredVo;
import com.roc.steelend.controller.vo.user.UpdateMsgVo;
import com.roc.steelend.pojo.User;
import com.roc.steelend.service.UserService;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "用户功能接口")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/v1/users")
    @ApiOperation(value = "查询所有用户信息",notes = "不需要值")
    public List<User> selectAllUser(HttpServletRequest request){
        List<User> users = userService.selectAllUser();
        return users;
    }

    @GetMapping("/v1/users2")
    @ApiOperation(value = "查询所有用户信息",notes = "不需要值")
    public List<User> selectAllUser2(){
        List<User> users = userService.selectAllUser2();
        return users;
    }

    @PostMapping("/registered/user")
    @ApiOperation(value = "注册用户消息",notes = "需要传两个对象，user和employee")
    public ReturnMsg registered(@RequestBody RegisteredVo registeredVo){

        Boolean aBoolean = userService.insertUser(registeredVo.getUser(), registeredVo.getEmployee());
        ReturnMsg returnMsg = new ReturnMsg();
        if (aBoolean){
            returnMsg.setMsg("注册成功");
            returnMsg.setCode(1);
            returnMsg.setData(registeredVo.getUser().getUserName());
        }else {
            returnMsg.setMsg("注册失败");
            returnMsg.setCode(0);
        }

        return returnMsg;
    }




    @PostMapping("/login/user")
    @ApiOperation(value = "进行登录",notes = "需要传一个用户对象")
    public ReturnMsg loginUser(@RequestBody User user){
        User boo = userService.loginUser(user);
        ReturnMsg msg = new ReturnMsg();
        if (boo==null){
            msg.setMsg("登录失败");
            msg.setCode(0);
        }else {
            msg.setMsg("登录成功");
            String realName = userService.checkUserRealName(boo.getUserId());
            Integer employeeId = userService.checkUserEmployeeId(boo.getUserId());
            HashMap<String, String> map = new HashMap<>();
            map.put("userName",boo.getUserName());
            map.put("userId",boo.getUserId().toString());
            map.put("realName",realName);
            map.put("employeeId",employeeId.toString());

            String token = JWTUtil.sendToken(map);
            HashMap<String, String> map1 = new HashMap<>();
            map1.put("userName",boo.getUserName());
            map1.put("token",token);
            msg.setData(map1);
            msg.setCode(1);
        }


        return msg;
    }


    @GetMapping("/v1/token/test")
    @ApiOperation(value = "判断token是否过期的小测试/并且判断返回用户名(并不是真实的姓名)",notes = "不需要值")
    public String tokenTest(HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT tokenMsg = JWTUtil.getToken(token);
        String userName = tokenMsg.getClaim("userName").asString();
        return userName;

    }

    @GetMapping("/v1/token/test2")
    @ApiOperation(value = "判断token是否过期的小测试",notes = "不需要值")
    public void tokenTest2(){

    }
    @GetMapping("/v1/token/test3")
    @ApiOperation(value = "更新用户名",notes = "不需要值")
    public String tokenTest3(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        HashMap<String, Object> hashMap = userService.test3Token(Integer.valueOf(employeeIdAsString)).get(0);
        HashMap<String, String> map = new HashMap<>();
        map.put("userName",String.valueOf(hashMap.get("user_name")));
        map.put("userId",String.valueOf(hashMap.get("user_id")));
        map.put("realName",String.valueOf(hashMap.get("real_name")));
        map.put("employeeId",String.valueOf(hashMap.get("employee_id")));
        String token1 = JWTUtil.sendToken(map);
        return token1;

    }

    @GetMapping("/v1/token/showRealName")
    @ApiOperation(value = "返回员工的真实姓名",notes = "不需要值")
    public String getUserRealName(HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String realName = decodedJWT.getClaim("realName").asString();
        return realName;
    }

    /**
     * 判断用户输入老密码是否正确
     */

    @PostMapping("/v1/changePassword/step1")
    @ApiOperation(value = "判断用户输入老密码是否正确",notes = "需要旧密码string")
    public ReturnMsg changePasswordStep1(@RequestBody String oldPassword,HttpServletRequest request){
       oldPassword= oldPassword.substring(0,oldPassword.length()-1);
        String token = request.getHeader("token");
        DecodedJWT dd = JWTUtil.getToken(token);
        String userIdToString = dd.getClaim("userId").asString();
        Integer userId = new Integer(userIdToString);
        Boolean changePasswordBefore = userService.changePasswordBefore(userId, oldPassword);
        ReturnMsg msg = new ReturnMsg();
        if (changePasswordBefore){
            msg.setMsg("旧密码正确");
            msg.setCode(1);
        }else {
            msg.setMsg("旧密码错误");
            msg.setCode(0);
        }
        return msg;
    }

    /**
     * 更新密码
     */
    @PostMapping("/v1/changePassword/step2")
    @ApiOperation(value = "进行密码更改",notes = "需要string密码")
    public ReturnMsg changePasswordStep2(@RequestBody String newPassword,HttpServletRequest request){
        newPassword= newPassword.substring(0,newPassword.length()-1);
        String token = request.getHeader("token");
        DecodedJWT dd = JWTUtil.getToken(token);
        String userIdToString = dd.getClaim("userId").asString();
        Integer userId = new Integer(userIdToString);
        Boolean changePassword = userService.changePassword(userId, newPassword);
        ReturnMsg msg = new ReturnMsg();
        if (changePassword){
            msg.setMsg("修改密码成功");
            msg.setCode(1);
        }else {
            msg.setMsg("修改密码失败");
            msg.setCode(0);
        }
        return msg;
    }

    /**
     * 获取用户权限
     */
    @GetMapping("/v1/user/power")
    @ApiOperation(value = "获取当前用户的权利")
    public Integer getUserPower(HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String userIdToString = decodedJWT.getClaim("userId").asString();
        Integer userId = new Integer(userIdToString);
        return     userService.checkUserPower(userId);
    }

    /**
     * 个人中心界面进行修改信息的展示值
     */
    @GetMapping("/v1/user/change/msg/detail")
    public List<HashMap<String, Object>> userChangeMsgDetail(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();

        return userService.userChangeMsgDetail(Integer.valueOf(employeeIdAsString));

    }
    /**
     * 查看该用户名是否重复
     */
    @PostMapping("/v1/user/check/name")
    public Boolean userCheckName(@RequestBody String userName){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        String userOldName = userService.userOldName(Integer.valueOf(employeeIdAsString));
       userName=userName.substring(0,userName.length()-1);
        if (userOldName.equals(userName)){
            return false;
        }

        return userService.userCheckName(userName);
    }


    /**
     *更新用户数据
     */
    @PostMapping("/v1/update/user/msg")
    public ReturnMsg updateUserMsg(@RequestBody UpdateMsgVo updateMsgVo){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return userService.updateUserMsg(updateMsgVo,Integer.valueOf(employeeIdAsString));
    }
}
